// 渲染“我的提问”列表
	
function renderMyQuestions(questions) {
  const tbody = document.querySelector('#my-questions-page tbody');
  tbody.innerHTML = ''; // 清空旧内容

  questions.forEach(question => {
    // 状态处理
    let statusHtml = '<span class="bg-yellow-100 text-yellow-800 text-xs px-2 py-1 rounded">待解决</span>';
    if (question.status === '已解决') {
      statusHtml = '<span class="bg-green-100 text-green-800 text-xs px-2 py-1 rounded">已解决</span>';
    }

    // 提问时间格式处理
    const createDate = new Date(question.createTime);
    const dateStr = createDate.toISOString().slice(0, 10);

    // 渲染行
    const tr = document.createElement('tr');
    tr.className = 'bg-white border-b hover:bg-gray-50';
    tr.innerHTML = `
      <td class="px-6 py-4 font-medium text-gray-900">${question.questionText}</td>
      <td class="px-6 py-4">${question.categoryName}</td>
      <td class="px-6 py-4">${statusHtml}</td>
      <td class="px-6 py-4">${dateStr}</td>
      <td class="px-6 py-4">
        <button class="text-red-500 text-sm hover:underline delete-btn" data-id="${question.id}">删除</button>
      </td>
    `;
    tbody.appendChild(tr);
  });

  // 绑定删除事件
  document.querySelectorAll('.delete-btn').forEach(btn => {
    btn.addEventListener('click', () => {
      const id = btn.getAttribute('data-id');
      if (confirm('确认删除这条提问吗？')) {
        axios.delete(`http://localhost:8080/api/questions/delete/${id}`)
          .then(res => {
            if (res.data.code === 200) {
              alert('删除成功');
              loadMyQuestions(); // 重新加载列表
            } else {
              alert(res.data.message || '删除失败');
            }
          })
          .catch(err => {
            console.error(err);
            alert('请求失败');
          });
      }
    });
  });
}
// 加载当前用户的提问
function loadMyQuestions() {
  const userId = localStorage.getItem('userid');
  if (!userId) {
   // alert('请先登录');
    return;
  }
  axios.get(`http://localhost:8080/api/questions/user/${userId}`)
    .then(res => {
      if (Array.isArray(res.data)) {
        renderMyQuestions(res.data);
      } else {
        alert('数据格式错误');
      }
    })
    .catch(err => {
      console.error(err);
      alert('获取数据失败');
    });
}

